# coding: UTF-8
from __future__ import unicode_literals

from django.db import models
from django.contrib.auth.admin import User

# Create your models here.


class Broker(models.Model):
    broker_keyword = models.CharField(u'交易商关键字',max_length=30,unique=True)
    broker_name = models.CharField(u'交易商名称',max_length=50)
    broker_des = models.TextField(u'交易商描述')

    def __unicode__(self):
        return self.broker_name

    class Meta:
        verbose_name = u'交易商'
        verbose_name_plural = u'交易商'


class TradeAccount(models.Model):

    user = models.ForeignKey(User,verbose_name= u'用户')
    broker = models.ForeignKey(Broker,to_field='broker_keyword',verbose_name=u'交易商')
    broker_account = models.CharField(u'交易账号',max_length=128)
    broker_pass = models.CharField(u'交易密码',max_length=128)
    cash =models.FloatField(u'账户现金')

    def __unicode__(self):
        return self.broker_account

    class Meta:
        verbose_name = u'交易账户'
        verbose_name_plural = u'交易账户'


class Strategy(models.Model):
    strategy_name = models.CharField(u'策略名称', max_length=50)
    strategy_desc = models.TextField(u'策略描述')
    strategy_keyword = models.CharField(u'策略关键字', max_length=30,help_text=u'策略关键字，用于关联后台的交易策略')

    def __unicode__(self):
        return self.strategy_name

    class Meta:
        verbose_name = u'交易策略'
        verbose_name_plural = u'交易策略'


class Portfolios(models.Model):

    trade_account = models.ForeignKey(TradeAccount,verbose_name=u'交易商账户')
    portfolios_name = models.CharField(u'组合名称', max_length=50)
    portfolios_desc = models.TextField(u'组合描述')
    init_cash = models.FloatField(u'初始化资金', help_text=u'所有组合的初始化资金不能大于交易账户的资金')
    cur_cash = models.FloatField(u'当前组合资金', editable=False)
    strategy = models.ForeignKey(Strategy,verbose_name=u'交易策略', null=True)
    strategy_parameter = models.TextField(u'策略参数', help_text=u'策略参数为json格式，后台策略根据策略参数运行该组合配置的策略',null=True)

    def __unicode__(self):
        return self.portfolios_name

    class Meta:
        verbose_name = u'交易组合'
        verbose_name_plural = u'交易组合'


